
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		body{background:#000;}
		#bor{height: 580px;width: 290px;border:3px solid #fff;margin: 50px 0 0 50px;float: left}
		#left{height: 116px;width:116px;border:3px solid #fff;float: left;margin-top: 50px;margin-left: 50px}
		.newkuai{height: 116px;width: 116px;}
		.xiaokuai{height: 27px;width:27px;float: left;border: 1px solid #eef;opacity: 0.2}
		.xiaolan{height: 27px;width:27px;background: #08f;float: left;border: 1px solid #fff;opacity: 0.9}
		.xiaolan1{height: 27px;width:27px;background: #0e0;float: left;border: 1px solid #fff;opacity: 0.9}
		#kaiui{height: 60px;width: 116px;position: absolute;top: 215px;left:58px;border: 3px solid #fff;font-size: 30px;color: #fff;text-align: center;line-height: 60px;cursor: pointer;font-style: italic;}
		#jifen{height: 60px;width: 116px;position: absolute;top: 320px;left:58px;border: 3px solid #fff;font-size: 30px;color: #fff;text-align: center;line-height: 60px;font-style: italic;}
		#uomy{height: 60px;width: 125px;position: absolute;top: 400px;left:55px;font-size: 1px;color: #fff;text-align: center;}
	</style>
	<script type="text/javascript" src="e.js"></script>
</head>
<body>
	<div id="left"></div>
	<div id="bor"></div>
	<div id="kaiui">START</div>
	<div id="jifen">0</div>
	<div id="uomy">
		<p>"A/D"键控制左右方向</p>
		<p>"W"键旋转方块</p>
		<p>每消除一层记10分</p>
		<p>由于比较懒，未加入"暂停"功能，游戏中不要点击"START"，方块下落速度会变快，也是由于比较懒，未屏蔽此BUG</p>
		<p>原生javascript编写，代码写的比较啰嗦，懒得去优化代码………各位大神勿喷</p>
		<p>By JST</p>
	</div>
	<script type="text/javascript">
		var left = document.getElementById('left');
		var kaiui = document.getElementById('kaiui');
		var qipan = document.getElementById('bor');
		var xiaoge = qipan.getElementsByTagName('div');
		var zoxiaoge = left.getElementsByTagName('div');
		var x = 0,y = 3;
		var xiangshang = null;
		var timer = null;
		var xzc = [];
		var jifen = 0;
		function radom() {
			var i = parseInt(Math.random() * 1000) % 7;
			switch (i) {
			case 0:
				xz = fang;
				break;
			case 1:
				xz = shu;
				break;
			case 2:
				xz = zol;
				break;
			case 3:
				xz = yol;
				break;
			case 4:
				xz = tu;
				break;
			case 5:
				xz = zopapa;
				break;
			case 6:
				xz = yopapa;
				break;
			};
			return xz;
		}
		function chushiwangge() {
			for (var i = 0; i < 200; i++) {
				if (xiaoge[i]) {
					qipan.removeChild(xiaoge[i]);
				}
				var xiaokuai = document.createElement('div');
				xiaokuai.className = 'xiaokuai';
				qipan.appendChild(xiaokuai);
			}
			addfang();
		}
		function addfang() {

			var newkuai = document.createElement('div');
			newkuai.className = 'newkuai';

			for (var i = 0; i < 16; i++) {
				if (zoxiaoge[i]) {
					left.removeChild(zoxiaoge[i]);
				}
				var xiaokuai = document.createElement('div');
				xiaokuai.className = 'xiaokuai';

				newkuai.appendChild(xiaokuai);
			}

			left.appendChild(newkuai);
			radom();

		}
		function gaileft() {
			var leftdiv = left.getElementsByTagName('div')[0];

			for (var i = 0; i < 16; i++) {
				var qx = parseInt(i / 4);
				var qy = i % 4;
				if (xz[qx][qy]) {
					leftdiv.getElementsByTagName('div')[i].className = "xiaolan";
				} 
				else {
					leftdiv.getElementsByTagName('div')[i].className = "xiaokuai";
				}
			}

		}
		function gaiwangge() {
			for (var i = 0; i < 200; i++) {
				var qx = parseInt(i / 10);
				var qy = i % 10;
				if (bor[qx][qy]==1) {
					xiaoge[i].className = "xiaolan";
				} 
				else if (bor[qx][qy]==2) {
					xiaoge[i].className = "xiaolan1";
				}
				else {
					xiaoge[i].className = "xiaokuai";
				}
			}
		}
		function gaibor() {
			xzc = xz;
			bor[x][y] = xz[0][0];
			bor[x][y + 1] = xz[0][1];
			bor[x][y + 2] = xz[0][2];
			bor[x][y + 3] = xz[0][3];
			bor[x + 1][y] = xz[1][0];
			bor[x + 1][y + 1] = xz[1][1];
			bor[x + 1][y + 2] = xz[1][2];
			bor[x + 1][y + 3] = xz[1][3];
			bor[x + 2][y] = xz[2][0];
			bor[x + 2][y + 1] = xz[2][1];
			bor[x + 2][y + 2] = xz[2][2];
			bor[x + 2][y + 3] = xz[2][3];
			bor[x + 3][y] = xz[3][0];
			bor[x + 3][y + 1] = xz[3][1];
			bor[x + 3][y + 2] = xz[3][2];
			bor[x + 3][y + 3] = xz[3][3];

			gaiwangge();
			radom();
			gaileft();
		}
		function zids() {
			var zhaox = [];
			var zhaoy = [];

			for (var i = 0; i < 200; i++) {
				var qx = parseInt(i / 10);
				var qy = i % 10;
				if (bor[qx][qy] == 1) {
					zhaox.push(qx);
					zhaoy.push(qy);
				}
			}
			for (var i = 0; i < 10; i++) {
				if (bor[19][i] == 1) {
					for (var i = 0; i <= 3; i++) {
						var xa = zhaox[i];
						var xb = zhaoy[i];
						bor[xa][xb] = 2;
						xiaochu();
					}

					gaibor();
					return false;
				}
			}
			for (var i = 0; i <= 3; i++) {
				var za = zhaox[i];
				var zb = zhaoy[i];
				if (bor[za + 1][zb] == 2) {
					for (var i = 0; i <= 3; i++) {
						var qqa = zhaox[i];
						var qqb = zhaoy[i];
						bor[qqa][qqb] = 2;
						xiaochu();
					}
					for (var i = 0; i < 40; i++) {
						var pjdx = parseInt(i / 10);
						var pjdy = i % 10;
						if (bor[pjdx][pjdy] == 2) {
							clearInterval(timer);
							window.onkeypress = null;
							for (var i = 0; i < 200; i++) {
								var qwx = parseInt(i / 10);
								var qwy = i % 10;
								bor[qwx][qwy] = 0;
							}
							alert('GAME OVER');
							document.getElementById('jifen').innerHTML = 0;
							gaiwangge();
							chushiwangge();
							jifen = 0;

							return;
						}
					}

					gaibor();
					return false;

				}
			}
			for (var i = 0; i < 200; i++) {
				var qx = parseInt(i / 10);
				var qy = i % 10;
				if (bor[qx][qy] != 2) {
					bor[qx][qy] = 0;
				}
			}
			for (var i = 0; i <= 3; i++) {
				//console.log(i)
				var ya = zhaox[i] + 1;
				var yb = zhaoy[i];
				bor[ya][yb] = 1;
			}

			gaiwangge();

		}
		function shijian(e) {
			var e = e || window.event;
			var zhaox = [];
			var zhaoy = [];

			for (var i = 0; i < 200; i++) {
				var qx = parseInt(i / 10);
				var qy = i % 10;
				if (bor[qx][qy] == 1) {
					zhaox.push(qx);
					zhaoy.push(qy);
				}
			}

			//console.log(zhaox,zhaoy)
			if (e.which == 97) {
				for (var i = 0; i < 20; i++) {
					if (bor[i][0] == 1) {
						return false;
					}
				}
				for (var i = 0; i <= 3; i++) {
					var zoa = zhaox[i];
					var zob = zhaoy[i];
					if (bor[zoa][zob - 1] == 2) {
						return false;
					}
				}
				for (var i = 0; i < 200; i++) {
					var qx = parseInt(i / 10);
					var qy = i % 10;
					if (bor[qx][qy] != 2) {
						bor[qx][qy] = 0;
					}
				}
				for (var i = 0; i <= 3; i++) {
					//console.log(i)
					var xa = zhaox[i];
					var xb = zhaoy[i] - 1;
					bor[xa][xb] = 1;
				}
			}
			if (e.which == 100) {
				for (var i = 0; i < 20; i++) {
					if (bor[i][9] == 1) {
						return false;
					}
				}
				for (var i = 0; i <= 3; i++) {
					var zoa = zhaox[i];
					var zob = zhaoy[i];
					if (bor[zoa][zob + 1] == 2) {
						return false;
					}
				}

				for (var i = 0; i < 200; i++) {
					var qx = parseInt(i / 10);
					var qy = i % 10;
					if (bor[qx][qy] != 2) {
						bor[qx][qy] = 0;
					}
				}
				for (var i = 0; i <= 3; i++) {
					//console.log(i)
					var ya = zhaox[i];
					var yb = zhaoy[i] + 1;
					bor[ya][yb] = 1;
				}

			}

			if (e.which == 115) {
				for (var i = 0; i < 10; i++) {
					if (bor[19][i] == 1) {
						for (var i = 0; i <= 3; i++) {
							var xa = zhaox[i];
							var xb = zhaoy[i];
							bor[xa][xb] = 2;
							xiaochu();
						}
						//gaileft()
						gaibor();
						return false;
					}
				}
				for (var i = 0; i <= 3; i++) {
					var za = zhaox[i];
					var zb = zhaoy[i];
					if (bor[za + 1][zb] == 2) {
						for (var i = 0; i <= 3; i++) {
							var qqa = zhaox[i];
							var qqb = zhaoy[i];
							bor[qqa][qqb] = 2;
							xiaochu();
						}

						//gaileft()
						for (var i = 0; i < 40; i++) {
							var pjdx = parseInt(i / 10);
							var pjdy = i % 10;
							if (bor[pjdx][pjdy] == 2) {
								clearInterval(timer);
								window.onkeypress = null;
								for (var i = 0; i < 200; i++) {
									var qwx = parseInt(i / 10);
									var qwy = i % 10;
									bor[qwx][qwy] = 0;
								}
								alert('GAME OVER');
								document.getElementById('jifen').innerHTML = 0;
								gaiwangge();
								chushiwangge();
								//break
								return
							}
						}
						gaibor();
						return false;

					}
				}
				for (var i = 0; i < 200; i++) {
					var qx = parseInt(i / 10);
					var qy = i % 10;
					if (bor[qx][qy] != 2) {
						bor[qx][qy] = 0;
					}
				}
				for (var i = 0; i <= 3; i++) {
					//console.log(i)
					var ya = zhaox[i] + 1;
					var yb = zhaoy[i];
					bor[ya][yb] = 1;
				}

			}
			if (e.which == 119) {

				switch (xzc) {
				case fang:
					xcz = fang;
					break;

				case tu:
					xzc = tu1;
					break;
				case tu1:
					xzc = tu2;
					break;
				case tu2:
					xzc = tu3;
					break;
				case tu3:
					xzc = tu;
					break;

				case shu:
					xzc = heng;
					break;
				case heng:
					xzc = shu;
					break;

				case zol:
					xzc = zol1;
					break;
				case zol1:
					xzc = zol2;
					break;
				case zol2:
					xzc = zol3;
					break;
				case zol3:
					xzc = zol;
					break;

				case yol:
					xzc = yol1;
					break;
				case yol1:
					xzc = yol2;
					break;
				case yol2:
					xzc = yol3;
					break;
				case yol3:
					xzc = yol;
					break;

				case zopapa:
					xzc = zopapa1;
					break;
				case zopapa1:
					xzc = zopapa;
					break;

				case yopapa:
					xzc = yopapa1;
					break;
				case yopapa1:
					xzc = yopapa;
					break;
				}

				xx = zhaox[0];
				yy = zhaoy[0];
				if (xx + 3 > 19 || xx + 1 > 19 || xx + 2 > 19 || xx > 19 || yy - 1 < 0 || yy < 0 || yy - 1 > 9 || yy > 9 || yy + 1 > 9 || yy + 2 > 9 || yy + 1 < 0 || yy + 2 < 0) {
					return false;
				}
				if (bor[xx][yy - 1] == 2 || bor[xx][yy] == 2 || bor[xx][yy + 1] == 2 || bor[xx][yy + 2] == 2 || bor[xx + 1][yy - 1] == 2 || bor[xx + 1][yy] == 2 || bor[xx + 1][yy + 1] == 2 || bor[xx + 1][yy + 2] == 2 || bor[xx + 2][yy - 1] == 2 || bor[xx + 2][yy] == 2 || bor[xx + 2][yy + 1] == 2 || bor[xx + 2][yy + 2] == 2 || bor[xx + 3][yy - 1] == 2 || bor[xx + 3][yy] == 2 || bor[xx + 3][yy + 1] == 2 || bor[xx + 3][yy + 2] == 2) {
					return false;
				}
				for (var i = 0; i < 200; i++) {
					var qxqx = parseInt(i / 10);
					var qyqy = i % 10;
					if (bor[qxqx][qyqy] != 2) {
						bor[qxqx][qyqy] = 0;
					}
				}
				bor[xx][yy - 1] = xzc[0][0];
				bor[xx][yy] = xzc[0][1];
				bor[xx][yy + 1] = xzc[0][2];
				bor[xx][yy + 2] = xzc[0][3];
				bor[xx + 1][yy - 1] = xzc[1][0];
				bor[xx + 1][yy] = xzc[1][1];
				bor[xx + 1][yy + 1] = xzc[1][2];
				bor[xx + 1][yy + 2] = xzc[1][3];
				bor[xx + 2][yy - 1] = xzc[2][0];
				bor[xx + 2][yy] = xzc[2][1];
				bor[xx + 2][yy + 1] = xzc[2][2];
				bor[xx + 2][yy + 2] = xzc[2][3];
				bor[xx + 3][yy - 1] = xzc[3][0];
				bor[xx + 3][yy] = xzc[3][1];
				bor[xx + 3][yy + 1] = xzc[3][2];
				bor[xx + 3][yy + 2] = xzc[3][3];
				//console.log(xzc);
			}
			gaiwangge();
		}
		function xiaochu() {
			for (var i = 0; i < 20; i++) {
				var arr = [];

				for (var ii = 0; ii < 10; ii++) {

					if (bor[i][ii] == 2) {
						arr.push(1);
					}
				}
				if (arr.length == 10) {
					jifen += 10;
					document.getElementById('jifen').innerHTML = jifen;
					for (var iii = 0; iii < 10; iii++) {
						bor[i][iii] = 0;
						xiangshang = i;
						//clearInterval(timer)
					}
					xiayi();
				}
			}

			//panduan()	
			//gaibor()	
		}
		function xiayi() {
			var shang = (xiangshang + 1) * 10;
			var xiaochuarr = [];
			var xiaochuarry = [];
			for (var i = 0; i < shang; i++) {
				var qx = parseInt(i / 10);
				var qy = i % 10;
				if (bor[qx][qy] == 2) {
					xiaochuarr.push(qx);
					xiaochuarry.push(qy);
				}
			}
			if (xiaochuarr.length != 0) {
				for (var i = 0; i < shang; i++) {
					var qxx = parseInt(i / 10);
					var qyy = i % 10;
					bor[qxx][qyy] = 0;
				}
				for (i = 0; i < xiaochuarr.length; i++) {
					var ix = xiaochuarr[i] + 1;
					var iy = xiaochuarry[i];
					bor[ix][iy] = 2;
				}
			}

		}

		chushiwangge();

		kaiui.onclick = start;
		function start() {
			xiangshang = null;
			window.onkeypress = shijian;
			gaibor();
			jifen = 0;
			timer = setInterval(zids, 400);
		}
	</script>
</body>
</html>